package springboottest.springboot;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;


@Aspect
@Configuration
public class SpringBootAop {
	@Pointcut("execution(* springboottest.springboot..*.*(..))")
	public void log() {
	}

	@Before("log()")
	public void doBefore(JoinPoint joinPoint) throws Throwable {
		// 接收到请求，记录请求内容

		System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "."
				+ joinPoint.getSignature().getName());

	}

	@After("log()")
	public void doAfter() {

		System.out.println("方法最后执行.....");

	}

	@Around("log() && @annotation(myLogTest)")
	public Object doAround(ProceedingJoinPoint pjp,MyLogTest myLogTest) {
		System.out.println("方法环绕start.....myLogTest"+myLogTest.desc());
		Object[] args=pjp.getArgs();
		for(int i=0;i<args.length;i++ ) {
			System.out.println(args[i]);
		}
		System.out.println("方法环绕start.....入参"+(args==null?"NULL":args[0].toString()));
		try {
			Object o = pjp.proceed();
			System.out.println("方法环绕proceed，结果是 :" + o);
			return o;
		} catch (Throwable e) {
			e.printStackTrace();
			return null;
		}

	}

	@AfterThrowing("log()")
	public void throwss(JoinPoint jp) {
		System.out.println("方法异常时执行.....");
	}

	@AfterReturning(returning = "ret", pointcut = "log()")
	public void doAfterReturning(Object ret) throws Throwable {
		// 处理完请求，返回内容
		System.out.println("方法的返回值 : " + ret);
	}

}
